package com.parktech.sun.domain.model.sunhz;

import com.parktech.sun.domain.model.ContentEntity;
import com.parktech.sun.utils.StringUtil;
import com.parktech.sun.utils.ValidatorRegexp;

import javax.persistence.*;
import javax.validation.constraints.Max;
import javax.validation.constraints.Pattern;
import java.util.List;

/**
 * User: Think
 * Date: 2010-12-20
 * Time: 1:29:52
 */
@Entity
@DiscriminatorValue("110")
public class Enterprise extends ContentEntity {

    private long categories;

    private boolean haveLogo;

    private boolean recommend;

    private boolean member;

    private String memberCode;

    @Max(255)
    private String homeUrl;

    @Transient
    public String getName() {
        return getTitle();
    }

    public void setName(String name) {
        setTitle(name);
    }

    @Max(100)
    @Pattern(regexp = ValidatorRegexp.PHONE)
    private String phone;

    @Max(100)
    @Pattern(regexp = ValidatorRegexp.EMAIL)
    private String email;

    private String linkman;

    @Pattern(regexp = ValidatorRegexp.PHONE)
    private String linkmanPhone;

    @Max(255)
    @Pattern(regexp = ValidatorRegexp.EMAIL)
    private String linkmanEmail;

    private List<EnterpriseCategoryProduct> productList;

    public long getCategories() {
        return categories;
    }

    public void setCategories(long categories) {
        this.categories = categories;
    }

    @Transient
    public boolean isSelect1() {
        return (categories & 1L) > 0;
    }

    @Transient
    public boolean isSelect2() {
        return (categories & 2L) > 0;
    }

    @Transient
    public boolean isSelect3() {
        return (categories & 4L) > 0;
    }

    @Transient
    public boolean isSelect4() {
        return (categories & 8L) > 0;
    }

    @Transient
    public boolean isSelect5() {
        return (categories & 16L) > 0;
    }

    @Transient
    public boolean isSelect6() {
        return (categories & 32L) > 0;
    }

    @Transient
    public Long[] getCategoryBits() {
        Long[] ret = new Long[32];
        for (int i = 0; i < 32; i++) {
            ret[i] = new Long((1 << i) & categories);
        }
        return ret;
    }

    public void setCategoryBits(Long[] categoryBits) {
        long categories = 0;
        for (long categoryBit : categoryBits) {
            categories = categories | categoryBit;
        }
        this.categories = categories;
    }

    public boolean isHaveLogo() {
        return haveLogo;
    }

    public void setHaveLogo(boolean haveLogo) {
        this.haveLogo = haveLogo;
    }

    public boolean isRecommend() {
        return recommend;
    }

    public void setRecommend(boolean recommend) {
        this.recommend = recommend;
    }

    public String getHomeUrl() {
        return homeUrl;
    }

    public void setHomeUrl(String homeUrl) {
        this.homeUrl = homeUrl;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getLinkman() {
        return linkman;
    }

    public void setLinkman(String linkman) {
        this.linkman = linkman;
    }

    public String getLinkmanPhone() {
        return linkmanPhone;
    }

    public void setLinkmanPhone(String linkmanPhone) {
        this.linkmanPhone = linkmanPhone;
    }

    public String getLinkmanEmail() {
        return linkmanEmail;
    }

    public void setLinkmanEmail(String linkmanEmail) {
        this.linkmanEmail = linkmanEmail;
    }

    public boolean isMember() {
        return member;
    }

    public void setMember(boolean member) {
        this.member = member;
    }

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
    @JoinTable(name = "SO_ENTERPRISE_PRODUCT_REF",
            joinColumns = {@JoinColumn(name = "enterprise_id", referencedColumnName = "ID", table = "SO_ENTERPRISE")},
            inverseJoinColumns = {@JoinColumn(name = "product_id", referencedColumnName = "id", table = "SO_ENTERPRISE_CATEGORY_PRODUCT")})
    public List<EnterpriseCategoryProduct> getProductList() {
        return productList;
    }

    public void setProductList(List<EnterpriseCategoryProduct> productList) {
        this.productList = productList;
    }

    public String getMemberCode() {
        if (memberCode == null) {
            return StringUtil.random(10);
        }
        return memberCode;
    }

    public void setMemberCode(String memberCode) {
        this.memberCode = memberCode;
    }

    @Transient
    public String getLocation() {
        return "enterprises";
    }
}